    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import Common.ChagesLanguage;
import Common.ConnectionDB;
import Common.FilePath;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author KIENLT
 */
public class ConfigConnection extends javax.swing.JDialog {

    Connection cnn = null;
    private final ResourceBundle rb;
    
    /**
     * Creates new form ConfigConnection
     */
    public ConfigConnection(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        ChagesLanguage.readFileLanguage();
        Locale locale = new Locale(ChagesLanguage.languageCode, ChagesLanguage.countryCode);
        rb = ResourceBundle.getBundle(ChagesLanguage.urlLanguage, locale);
        lblConnectDatabase.setText(rb.getString("ConnectDatabase"));
        lblDatabase.setText(rb.getString("Database" +":"));
        lblLogin.setText(rb.getString("Login")+":");
        lblPassword.setText(rb.getString("Password") +":");
        lblPost.setText(rb.getString("Post") +":");
        btnConnect.setText(rb.getString("Connect")+":");
        btnCancel.setText(rb.getString("Cancel"));
        this.setTitle(rb.getString("ConfigConnection"));
        this.setLocationRelativeTo(null);
        try {
            setIconImage(Toolkit.getDefaultToolkit().getImage(new File("src\\Images\\Icon-Form.png").getCanonicalPath()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void writeFile() {
        try {
            cnn = ConnectionDB.ConnectDB(txtPost.getText(), txtDatabase.getText(), txtLogin.getText(), txtPassword.getText());
            if(cnn != null){
                FileOutputStream fo = new FileOutputStream(FilePath.getFilePath(),false);
                PrintWriter pw = new PrintWriter(fo);
                pw.println(txtPost.getText());
                pw.println(txtDatabase.getText());
                pw.println(txtLogin.getText());
                pw.println(txtPassword.getText());
                pw.close();
                this.dispose();
            }
        } catch (FileNotFoundException ex) {
            
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblDatabase = new javax.swing.JLabel();
        lblPost = new javax.swing.JLabel();
        lblConnectDatabase = new javax.swing.JLabel();
        txtPost = new javax.swing.JTextField();
        lblLogin = new javax.swing.JLabel();
        btnConnect = new javax.swing.JButton();
        lblPassword = new javax.swing.JLabel();
        txtLogin = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        txtDatabase = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configuration connected databases");
        setResizable(false);

        lblDatabase.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblDatabase.setText("Database");

        lblPost.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPost.setText("Post");

        lblConnectDatabase.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblConnectDatabase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/connect_to_db_64.png"))); // NOI18N
        lblConnectDatabase.setText("Connection Database");

        txtPost.setPreferredSize(new java.awt.Dimension(6, 30));

        lblLogin.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblLogin.setText("Login");

        btnConnect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/icon-update.png"))); // NOI18N
        btnConnect.setText("Connect");
        btnConnect.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnConnect.setPreferredSize(new java.awt.Dimension(100, 35));
        btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectActionPerformed(evt);
            }
        });

        lblPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPassword.setText("Password");

        txtLogin.setPreferredSize(new java.awt.Dimension(6, 30));

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/icon-cancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnCancel.setPreferredSize(new java.awt.Dimension(100, 35));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        txtDatabase.setPreferredSize(new java.awt.Dimension(6, 30));

        txtPassword.setPreferredSize(new java.awt.Dimension(6, 30));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblConnectDatabase)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lblLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblDatabase, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                            .addComponent(lblPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblPost, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtPost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblConnectDatabase)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblLogin))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPassword)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblDatabase)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPost))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
        writeFile();
    }//GEN-LAST:event_btnConnectActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnConnect;
    private javax.swing.JLabel lblConnectDatabase;
    private javax.swing.JLabel lblDatabase;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPost;
    private javax.swing.JTextField txtDatabase;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPost;
    // End of variables declaration//GEN-END:variables
}
